//
//  SLPOPSpringController.m
//  Objective-C
//
//  Created by 陆承东 on 2018/7/7.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLPOPSpringController.h"

#import <pop/POP.h>

@interface SLPOPSpringController ()

@property (nonatomic, weak) UIView *blueBallView;

@end

@implementation SLPOPSpringController

- (void)initNavBar {
    self.navTitle = @"POP SpringAnimation";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavBar];
    
    [self initView];
}

- (void)initView {
    CGFloat blueBallViewWH = 30.0;
    UIView *blueBallView = [[UIView alloc] initWithFrame:CGRectMake(100, 200, blueBallViewWH, blueBallViewWH)];
    self.blueBallView = blueBallView;
    blueBallView.backgroundColor = [UIColor blueColor];
    blueBallView.layer.cornerRadius = blueBallViewWH / 2.0;
    blueBallView.layer.masksToBounds = true;
    [self.view addSubview:blueBallView];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    POPSpringAnimation *spring = [POPSpringAnimation animationWithPropertyNamed:kPOPViewScaleXY];
    /**
     @abstract The velocity value.
     @discussion Set before animation start to account for initial velocity.
                 Expressed in change of value units per second.
     */
    //spring.velocity = [NSValue valueWithCGPoint:CGPointMake(0.01, 1000.0)];
    
    /**
     @abstract The effective bounciness.
     @discussion Use in conjunction with 'springSpeed' to change animation effect.
                 Values are converted into corresponding dynamics constants.
                 Higher values increase spring movement range resulting in more oscillations and springiness.
                 Defined as a value in the range [0, 20]. Defaults to 4.
     */
    spring.springSpeed = 20.00;         // 12.00
    
    /**
     @abstract The effective speed.
     @discussion Use in conjunction with 'springBounciness' to change animation effect.
                 Value are converted into corresponding dynamics constants.
                 Higher values increase the dampening power of the spring resulting in a faster initial velocity and more rapid bounce slowdown.
                 Defined as a value in the range [0, 20]. Defaults to 12.
     */
    spring.springBounciness = 20.00;    // 4.00
    
    /**
     @abstract The mass used in the dynamics simulation.
     @discussion Can be used over bounciness and speed for finer grain tweaking of animation effect.
     */
//    spring.dynamicsMass = 1.00;         // 1.00
    
    /**
     @abstract The tension used in the dynamics simulation.
     @discussion Can be used over bounciness and speed for finer grain tweaking of animation effect.
     */
//    spring.dynamicsTension = 1.00;      // 513.00
    
    /**
     @abstract The friction used in the dynamics simulation.
     @discussion Can be used over bounciness and speed for finer grain tweaking of animation effect.
     */
//    spring.dynamicsFriction = 1.00;     // 13.356488
    
    spring.fromValue = [NSValue valueWithCGPoint:CGPointMake(1.0, 1.0)];
//    spring.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 100, 100)];
    spring.toValue = [NSValue valueWithCGPoint:CGPointMake(5.0, 5.0)];
    spring.beginTime = CACurrentMediaTime() + 1.0;
    SLog(@"\nvelocity: %@\nspringBounciness: %f\nspringSpeed: %f\ndynamicsMass: %f\ndynamicsTension: %f\ndynamicsFriction: %f", spring.velocity, spring.springBounciness, spring.springSpeed, spring.dynamicsMass, spring.dynamicsTension, spring.dynamicsFriction);
    [self.blueBallView pop_addAnimation:spring forKey:@"spring"];
    
    POPSpringAnimation *spring1 = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
    spring1.fromValue = [NSValue valueWithCGPoint:CGPointMake(115, 215)];
    spring1.toValue = [NSValue valueWithCGPoint:CGPointMake(215, 315)];
    spring1.springSpeed = 20.0;
    [self.blueBallView pop_addAnimation:spring1 forKey:@"spring1"];
}

@end
